//
//  ZJKEmoticonKeyboard.swift
//  MicroBlog
//
//  Created by VAD on 16/4/27.
//  Copyright © 2016年 朱江坤. All rights reserved.
//

import UIKit

class ZJKEmoticonKeyboard: UIView {

  
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        // 设置要执行的闭包
        toolBar.emoticonButtonClickClosure = { (type)->() in
            print(type)
        }
        //添加子控件
        addSubview(toolBar)
        addSubview(emoticonCollectionView)
        
        // 添加约束
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let layout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: SCREENW, height: self.frame.height - toolBar.frame.height)
    }
    
    
    // MARK: - 懒加载控件
    
    private lazy var emoticonCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        // layout.itemSize = CGSize(width: SCREENW, height: 216-37)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        // 设置布局方向
        layout.scrollDirection = .Horizontal
        
        let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        // 注册cell
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        // 设置数据源
        collectionView.dataSource = self
        // 去掉弹簧效果
        collectionView.bounces = false
        // 分页
        collectionView.pagingEnabled = true
        // 隐藏滚动条
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    private lazy var toolBar: ZJKEmoticonToolBar = ZJKEmoticonToolBar(frame: CGRectZero)

}
extension ZJKEmoticonKeyboard: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 从缓存中取
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        
        cell.backgroundColor = RandomColor
        
        return cell
    }

}
